package com.life.controller;

import java.util.ArrayList;

import com.life.model.LifeModel;
import com.life.model.event.ModelBoardRuleSetEvent;
import com.life.model.event.ModelBoardUpdateEvent;
import com.life.rule.ILifeRule;
import com.life.view.AbstractAdapter;

public class StatusController extends AbstractController {

	AbstractAdapter generationAdapter = null;
	AbstractAdapter currentRuleAdapter = null;
	AbstractAdapter resetAdapter = null;
	AbstractAdapter populationAdapter = null;
	
	public StatusController(LifeModel model, ArrayList<AbstractAdapter> adapterList) {
		super(model, adapterList);
		
		generationAdapter = adapterList.get(0);
		currentRuleAdapter = adapterList.get(1);
		resetAdapter = adapterList.get(2);
		populationAdapter = adapterList.get(3);
		
		currentRuleAdapter.set("Current Rule: Life(23/3)");
		
		
		model.addModelEventsListener(this);
		
	}

	@Override
	public void doUpdate(AbstractAdapter adapter, Object data) {
		
		if(adapter == resetAdapter)
		{
			setGeneration(0);
		}		
	}
	
	public void modelBoardUpdated(ModelBoardUpdateEvent event) {
		
		if(theModel.getCurrentRule() != null)
		{
			int val = theModel.getGenerationCount();
			setGeneration(val);
			setPopulation(theModel.getLifeCount());
		}
	}
	
	public void setPopulation(int val)
	{
	    populationAdapter.set("Population: " + val);
	}
	
	
	public void setCurrentRule(ILifeRule rule)
	{
		currentRuleAdapter.set("Current Rule: " +rule + ""+ rule.getRuleString());
	}
	
	public void setGeneration(int val)
	{
		generationAdapter.set("Generation: " + val);
		
	}

	public void modelRuleSet(ModelBoardRuleSetEvent event)
	{
		setCurrentRule(event.getRule());
	}
	
}

